const dbutil = require('../../../libs/MyDB');
const uuid = require('node-uuid');

module.exports = {
    addOrderTable: async function (data) {
        let orderObj = {}
        let orderId = 0;
        let platform = '622'; //平台变化 622代表当前系统平台的码
        let r1 = Math.floor(Math.random() * 10); // 获取两个0-9的随机数
        let r2 = Math.floor(Math.random() * 10);
        let sysDate = new Date().Format('yyyyMMddhhmmss'); // 生成系统时间
        let order_number = platform + r1 + sysDate + r2; // 生成订单编号
        let order = {
            order_number: order_number,
            order_payment_status: '未提交',
            user_id: data.user_id,
            table_id: data.table_id
        };
        await dbutil.add('res_order', order)
            .then(
                res => {
                    orderId = res.insertId;
                }
            )
        await dbutil.multiSelect('res_order', '*', `order_id = ${orderId}`)
            .then(
                res => {
                    orderObj = res[0];
                }
            )
        await dbutil.update('res_table', `table_id=${data.table_id}`, { table_usage: '使用中', user_id: data.user_id })

        return {
            code: 200,
            msg: "查询成功",
            data: orderObj,
        }
    },
    addRow: async function (data) {
        let number;
        await dbutil.multiSelect('res_tabletype', 'row_number', `tabletype_number='${data.type}'`)
            .then(
                res => {
                    number = res[0].row_number + 1;
                }
            )
        await dbutil.update('res_tabletype', `tabletype_number='${data.type}'`, { row_number: number })
        number = data.type + '0' + number;
        delete data.type;
        data.row_number = number;
        let row = {};
        let rowId = 0;
        await dbutil.add('res_row', data)
            .then(
                res => {
                    rowId = res.insertId;
                }
            )
        await dbutil.multiSelect('res_row', '*', `row_id = ${rowId}`)
            .then(
                res => {
                    row = res[0];
                }
            )
        let rows = [];
        await dbutil.multiSelect('res_row', '*', `tabletype_id=${data.tabletype_id}`).then(
            res => {
                rows = res;
            }
        )
        row.count = rows.length;
        return {
            code: 200,
            msg: "查询成功",
            data: row,
        }
    },
    getRow: async function (data) {
        let row = null;
        await dbutil.multiSelect('res_row', '*', `user_id=${data.user_id}`)
            .then(
                res => {
                    row = res[0];
                }
            )
        if (row != null) {
            await dbutil.multiSelect('res_tabletype', '*', `tabletype_id=${row.tabletype_id}`)
                .then(
                    res => {
                        row.tabletype_people = res[0].tabletype_people;
                    }
                )
        } else {
            let order;
            await dbutil.multiSelect('res_order', '*', `user_id=${data.user_id} and order_payment_status = '未提交'`)
                .then(
                    res => {
                        order = res[0]
                    }
                )

            return {
                code: 201,
                msg: "查询成功",
                data: order,
            }
        }

        return {
            code: 200,
            msg: "查询成功",
            data: row,
        }
    }
}